package mesweb.mesweb.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.Utils.QueryWrapperUtil;
import mesweb.mesweb.entity.MesTechnology;
import mesweb.mesweb.service.IMesTechnologyService;
import mesweb.mesweb.vo.MesTechnologyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */

@Api(tags="标准工艺")
@RestController
@RequestMapping("/mes/mesTechnology")
public class MesTechnologyController {
    @Autowired
    private IMesTechnologyService mesTechnologyService;

    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAllTechnologys(
            @ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
        List<MesTechnology> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = mesTechnologyService.list();
        } else {
            QueryWrapper<MesTechnology> queryWrapper = new QueryWrapper<>();
           // queryWrapper.like("Therbligs_Type_Name", name);
            data = mesTechnologyService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }

    @GetMapping("/GetAllByRalation")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAllTechnologies(
            @ApiParam("工艺组Id") @RequestParam( value = "technologySetId",required = false) Long technologySetId,
            @ApiParam("工段") @RequestParam( value = "workSectionId",required = false) Long workSectionId
            ) {
        List<MesTechnologyVo> data;
        QueryWrapper<MesTechnology> queryWrapper = new QueryWrapper<>();
        if (workSectionId >0L) {
           queryWrapper.eq("c.work_section_id",workSectionId);
        } else if(technologySetId>0) {
            queryWrapper.eq("c.id",technologySetId);
        }
        data = mesTechnologyService.getTechnologies(queryWrapper);
        return CommonResult.success(data);
    }

    /**
     * 分页列表查询
     *
     * @param mesTechnology
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value="标准工艺-分页列表查询", notes="标准工艺-分页列表查询")
    @GetMapping(value = "/list")
    public CommonResult<?> queryPageList(MesTechnology mesTechnology,
                                         @RequestParam(name="page", defaultValue="1") Integer page,
                                         @RequestParam(name="size", defaultValue="10") Integer size,
                                         HttpServletRequest req) {
        QueryWrapperUtil queryWrapperUtil = new QueryWrapperUtil<>();
        QueryWrapper<MesTechnology> queryWrapper =  queryWrapperUtil.build(MesTechnology.class,mesTechnology);
        Page<MesTechnologyVo> mesTechnologyVoPage = new Page<>(page, size);
        IPage<MesTechnologyVo> pageList = mesTechnologyService.getTechnologys(mesTechnologyVoPage, queryWrapper);
        return CommonResult.success(pageList);
    }
    /**
     * 编辑
     *
     * @param mesTechnology
     * @return
     */
    @ApiOperation(value="标准工艺-编辑", notes="标准工艺-编辑")
    @PostMapping(value = "/save")
    public CommonResult<?> edit(@RequestBody MesTechnology mesTechnology) {

        boolean result = false;
        if(mesTechnology.id == 0L) {
            QueryWrapper<MesTechnology> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("Technology_No",mesTechnology.getTechnologyNo());
            queryWrapper.or().eq("technology_name",mesTechnology.getTechnologyName());
            MesTechnology mesTechnology1 = mesTechnologyService.getOne(queryWrapper);
            if(mesTechnology1 != null){
                String msg = StrUtil.format("编号{}或名称{}已经存在！",mesTechnology1.getTechnologyNo(),mesTechnology1.getTechnologyName());
                throw new ApiException(msg);
            }
            result = mesTechnologyService.save(mesTechnology);
        }
        else
        {
            result = mesTechnologyService.updateById(mesTechnology);
        }
        return CommonResult.success(mesTechnology.id);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value="标准工艺-通过id删除", notes="标准工艺-通过id删除")
    @DeleteMapping(value = "/delete")
    public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
        mesTechnologyService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value="标准工艺-批量删除", notes="标准工艺-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@ApiParam("标准工艺IDs") @RequestBody List<Long> ids) {
        this.mesTechnologyService.removeByIds(ids);
        return CommonResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="标准工艺-通过id查询", notes="标准工艺-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
        MesTechnology mesTechnology = mesTechnologyService.getById(id);
        return CommonResult.success(mesTechnology);
    }
}
